import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import axios from '@/plugins/axiosInstance.js'

import App from './App.vue'
import router from './router'
import store from './store'

axios.defaults.baseURL = 'http://localhost:8443/api' // 请求基础地址
axios.defaults.withCredentials = true // Cookie跨域
//post请求头

//允许跨域携带cookie信息
axios.defaults.withCredentials = true
//设置超时
axios.defaults.timeout = 15000

// 拦截器——未登录就拦截路由
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (store.state.user.username) {
      next()
    } else {
      next({
        path: 'login',
        query: { redirect: to.fullPath },
      })
    }
  } else {
    next()
  }
})

const app = createApp(App)

app.use(store).use(router).use(ElementPlus).mount('#app')
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.config.globalProperties.$http = axios
